<template>
	<view class="countdown-book">
		<TopNavBar backgroundImage="../../static/bg.jpg" backgroundColor="#071934" :navHeight="100"
			leftIcon="/static/left-icon.png" leftText="返回" centerText="时光悦等" rightIcon="/static/right-icon.png"
			rightText="更多" @left-click="handleLeftClick" @right-click="handleRightClick" />

		<!-- 笔记本网格视图 -->
		<scroll-view scroll-y class="notebook-grid">
			<view class="grid-container">
				<!-- 添加新时光悦等 -->
				<view class="grid-item add-new" @tap="showAddNew">
					<text class="iconfont icon-plus">&#xe6b9;</text>
					<text class="item-label">添加新时光悦等</text>
				</view>

				<!-- 现有时光悦等 -->
				<view v-for="book in notebooks" :key="book.id" class="grid-item" :class="book.type"
					@tap="openNotebook(book)">
					<view class="book-cover">
						<text class="book-icon">{{ book.icon }}</text>
						<text class="book-count">{{ book.count }}</text>
					</view>
					<text class="item-label">{{ book.name }}</text>
				</view>
			</view>
		</scroll-view>

		<!-- 列表视图 -->
		<view class="list-view">
			<view v-for="category in categories" :key="category.id" class="list-item" @tap="selectCategory(category)">
				<view class="item-left">
					<text class="category-icon" :class="category.iconClass">{{ category.icon }}</text>
					<text class="category-name">{{ category.name }}</text>
					<view v-if="category.count" class="count-badge">{{ category.count }}</view>
				</view>
				<view class="item-right">
					<text v-if="category.daysLeft" class="days-left">{{ category.daysLeft }}</text>
					<text class="iconfont icon-arrow">&#xe6a3;</text>
				</view>
			</view>

			<!-- 工具分割线 -->
			<view class="divider">工具</view>

			<!-- 工具项 -->
			<view class="tools-section">
				<view class="tool-item" @tap="openArchive">
					<text class="tool-icon">📁</text>
					<text class="tool-name">归档与隐藏</text>
				</view>
				<view class="tool-row">
					<view class="tool-item" @tap="openCalculator">
						<text class="tool-icon">📅</text>
						<text class="tool-name">日期计算器</text>
					</view>
					<view class="tool-item" @tap="openMilestone">
						<text class="tool-icon">⭐</text>
						<text class="tool-name">里程碑</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部标签栏 -->
		<tabbar @choseTabbarMethod="choseTabbarMethod"></tabbar>

		<!-- 添加新时光悦等弹窗 -->
		<uni-popup ref="addPopup" type="center">
			<view class="popup-content">
				<view class="popup-header">
					<text class="popup-title">创建新的时光悦等</text>
					<text class="popup-close" @tap="hideAddNew">×</text>
				</view>
				<view class="popup-body">
					<input class="input-field" v-model="newNotebook.name" placeholder="给时光悦等起个名字" maxlength="20" />
					<view class="type-selector">
						<view v-for="item in notebookTypes" :key="item.id" class="type-item"
							:class="{ selected: newNotebook.type === item.id }" @tap="selectType(item)">
							<text class="type-icon">{{ type.icon }}</text>
							<text class="type-name">{{ type.name }}</text>
						</view>
					</view>
					<button class="confirm-button" @tap="createNotebook">创建时光悦等</button>
				</view>
			</view>
		</uni-popup>
		<u-modal :show="show" :title="title">
			<view class="slot-content">
				<u--form labelPosition="left" :model="notebook" ref="uForm">
					<u-form-item label="标题" prop="notebook.name" borderBottom ref="item1">
						<u--input v-model="notebook.name" border="none"></u--input>
					</u-form-item>
					<u-form-item label="内容" prop="notebook.content" borderBottom ref="item2">
						<u--input v-model="notebook.content" border="none"></u--input>
					</u-form-item>
					<!-- 选择时间 -->
					<u-form-item label="时间" prop="notebook.time" borderBottom ref="item3">
						<u-datetime-picker :show="showTime" v-model="notebook.time" mode="datetime"></u-datetime-picker>
					</u-form-item>
				</u--form>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import tabbar from '@/components/tabbar/tabbar.vue';
	import {
		addData
	} from "../../utils/allData.js";
	import TopNavBar from '@/components/topNavigationBar/topNavigationBar.vue';
	export default {
		components: {
			TopNavBar,
			tabbar
		},
		data() {
			return {
				bgColor: "#eee",
				statusBarHeight: 0,
				currentTab: 'home',
				notebooks: [{
						id: 1,
						name: '生活',
						type: 'life',
						icon: '📖',
						count: 2
					},
					{
						id: 2,
						name: '工作',
						type: 'work',
						icon: '💼',
						count: 0
					},
					{
						id: 3,
						name: '纪念日',
						type: 'anniversary',
						icon: '🎂',
						count: 1
					},
					{
						id: 4,
						name: '111',
						type: 'custom',
						icon: '📝',
						count: 0
					}
				],
				categories: [{
						id: 'all',
						name: '全部',
						icon: '🌍',
						count: 3,
						iconClass: 'icon-globe'
					},
					{
						id: 'life',
						name: '生活',
						icon: '❤️',
						count: 2,
						iconClass: 'icon-heart'
					},
					{
						id: 'work',
						name: '工作',
						icon: '⭐',
						count: 0,
						iconClass: 'icon-star'
					},
					{
						id: 'anniversary',
						name: '纪念日',
						icon: '💕',
						count: 1,
						iconClass: 'icon-hearts',
						daysLeft: 'Google成立已经9577天'
					},
					{
						id: '111',
						name: '111',
						icon: '💼',
						count: 0,
						iconClass: 'icon-briefcase'
					}
				],
				tabs: [{
						id: 'calendar',
						name: '日历',
						icon: '📅',
						iconClass: 'icon-calendar'
					},
					{
						id: 'star',
						name: '收藏',
						icon: '⭐',
						iconClass: 'icon-star'
					},
					{
						id: 'explore',
						name: '发现',
						icon: '🪐',
						iconClass: 'icon-planet'
					},
					{
						id: 'profile',
						name: '我的',
						icon: '👤',
						iconClass: 'icon-profile'
					}
				],
				newNotebook: {
					name: '',
					type: ''
				},
				notebookTypes: [{
						id: 'life',
						name: '生活',
						icon: '📖'
					},
					{
						id: 'work',
						name: '工作',
						icon: '💼'
					},
					{
						id: 'anniversary',
						name: '纪念日',
						icon: '🎂'
					},
					{
						id: 'custom',
						name: '自定义',
						icon: '📝'
					}
				],
				show: false,
				title: '启幕前奏',
				content: "1111",
				showTime: false,
				notebook: {
					name: '',
					content: '',
					time: ''
				}
			}
		},
		onLoad() {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
			console.log(addData, 'addData');

		},
		methods: {
			handleLeftClick() {
				uni.navigateBack();
			},
			handleRightClick() {
				console.log('Right button clicked');
			},
			showAddNew() {
				this.$refs.addPopup.open()
			},
			hideAddNew() {
				this.$refs.addPopup.close()
				this.newNotebook = {
					name: '',
					type: ''
				}
			},
			selectType(type) {
				this.newNotebook.type = type.id
			},
			createNotebook() {
				if (!this.newNotebook.name.trim()) {
					uni.showToast({
						title: '请输入时光悦等名称',
						icon: 'none'
					})
					return
				}
				if (!this.newNotebook.type) {
					uni.showToast({
						title: '请选择时光悦等类型',
						icon: 'none'
					})
					return
				}
				const data = {
					id: Date.now(),
					name: this.newNotebook.name,
					type: this.newNotebook.type,
					icon: this.notebookTypes.find(t => t.id === this.newNotebook.type).icon,
					count: 0
				}
				addData(data)
				this.hideAddNew()
				uni.showToast({
					title: '创建成功',
					icon: 'success'
				})
			},
			openNotebook(book) {
				uni.navigateTo({
					url: `/pages/notebook/detail?id=${book.id}`
				})
			},
			selectCategory(category) {
				console.log(category, 'category');
				this.show = true

			},
			switchTab(tabId) {
				this.show = true

				console.log(tabId, 'tabId');
			},
			openSettings() {
				uni.navigateTo({
					url: '/pages/settings/index'
				})
			},
			openArchive() {
				uni.navigateTo({
					url: '/pages/archive/index'
				})
			},
			openCalculator() {
				uni.navigateTo({
					url: '/pages/tools/calculator'
				})
			},
			openMilestone() {
				uni.navigateTo({
					url: '/pages/tools/milestone'
				})
			}
		}
	}
</script>

<style>
	.countdown-book {
		min-height: 100vh;
		background-color: #f8f9fa;
	}

	.custom-nav {
		background: linear-gradient(135deg, #4a90e2, #357abd);
		color: #ffffff;
	}

	.nav-content {
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 16px;
	}

	.app-title {
		font-size: 18px;
		font-weight: 600;
	}

	.nav-right {
		display: flex;
		gap: 16px;
	}

	.icon-btn {
		width: 32px;
		height: 32px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.notebook-grid {
		padding: 16px;
	}

	.grid-container {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16px;
	}

	.grid-item {
		background: #ffffff;
		border-radius: 12px;
		padding: 20px;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
	}

	.add-new {
		border: 2px dashed #e0e0e0;
		background: transparent;
	}

	.book-cover {
		width: 60px;
		height: 80px;
		background: #f5f7fa;
		border-radius: 8px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-bottom: 8px;
	}

	.book-icon {
		font-size: 24px;
		margin-bottom: 4px;
	}

	.book-count {
		font-size: 14px;
		color: #666666;
	}

	.item-label {
		font-size: 14px;
		color: #333333;
	}

	.list-view {
		background: #ffffff;
		border-radius: 16px 16px 0 0;
		padding: 16px;
		margin-top: 16px;
	}

	.list-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 16px;
		border-bottom: 1px solid #f5f7fa;
	}

	.item-left {
		display: flex;
		align-items: center;
		gap: 12px;
	}

	.category-icon {
		font-size: 20px;
	}

	.category-name {
		font-size: 16px;
		color: #333333;
	}

	.count-badge {
		background: #f0f0f0;
		border-radius: 10px;
		padding: 2px 8px;
		font-size: 12px;
		color: #666666;
	}

	.item-right {
		display: flex;
		align-items: center;
		gap: 8px;
		color: #999999;
	}

	.days-left {
		font-size: 14px;
	}

	.divider {
		padding: 16px;
		color: #999999;
		font-size: 14px;
	}

	.tools-section {
		padding: 0 16px;
	}

	.tool-row {
		display: flex;
		gap: 16px;
	}

	.tool-item {
		flex: 1;
		padding: 16px;
		background: #f8f9fa;
		border-radius: 12px;
		margin-bottom: 16px;
		display: flex;
		align-items: center;
		gap: 8px;
	}

	.tool-icon {
		font-size: 20px;
	}

	.tool-name {
		font-size: 14px;
		color: #333333;
	}

	.tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 50px;
		background: #ffffff;
		display: flex;
		justify-content: space-around;
		align-items: center;
		border-top: 1px solid #f0f0f0;
	}

	.tab-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 4px;
	}

	.tab-icon {
		font-size: 20px;
	}

	.tab-label {
		font-size: 12px;
		color: #666666;
	}

	.tab-item.active {
		color: #4a90e2;
	}

	.popup-content {
		background: #ffffff;
		border-radius: 16px;
		width: 80vw;
	}

	.popup-header {
		padding: 16px;
		border-bottom: 1px solid #f0f0f0;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.popup-title {
		font-size: 18px;
		font-weight: 600;
		color: #333333;
	}

	.popup-close {
		font-size: 24px;
		color: #999999;
		padding: 8px;
	}

	.popup-body {
		padding: 16px;
	}

	.input-field {
		width: 100%;
		height: 44px;
		border: 1px solid #e0e0e0;
		border-radius: 8px;
		padding: 0 16px;
		margin-bottom: 16px;
		font-size: 16px;
	}

	.type-selector {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 12px;
		margin-bottom: 16px;
	}

	.type-item {
		padding: 12px;
		border: 1px solid #e0e0e0;
		border-radius: 8px;
		display: flex;
		align-items: center;
		gap: 8px;
	}

	.type-item.selected {
		border-color: #4a90e2;
		background: rgba(74, 144, 226, 0.05);
	}

	.type-icon {
		font-size: 20px;
	}

	.type-name {
		font-size: 14px;
		color: #333333;
	}

	.confirm-button {
		width: 100%;
		height: 44px;
		background: #4a90e2;
		color: #ffffff;
		border-radius: 8px;
		font-size: 16px;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>